//
//  YAYIWaitPayController.m
//  YAYIDoctor
//
//  Created by hxp on 16/6/13.
//
//

#import "YAYIWaitPayController.h"
#import "YAYINoPayOrderModel.h"
#import "YAYIOrderdetailController.h"
#import "YAYIApliyDetailController.h"
#import "YAYIWaitPayCell.h"

@interface YAYIWaitPayController ()

// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic,strong)NSMutableArray *dataAry;

/** 分页相关 */
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYIWaitPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataAry = [NSMutableArray array];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(108, 0, 20, 0);
    
//    [self createDataSource];
    
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself createDataSource];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreOrder];
    }];
//    [self.tableView.mj_footer setAutomaticallyHidden:YES];
    
    // 进入执行下拉
    [self.tableView.mj_header beginRefreshing];
}


/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有数据";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-90);
    }];
}


#pragma mark - 获取数据
-(void)createDataSource
{
    
    [self.dataAry removeAllObjects];
    
    [YAYINetWorking  getWithURL:noPay_list_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回：%@", dict);
        
        NSString *status = dict[@"status"];
        if ([status isEqualToString:@"success"]) {
            NSArray *data = dict[@"data"];
            for (NSDictionary *obj in data) {
                YAYINoPayOrderModel *model = [YAYINoPayOrderModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataAry  addObject:model];
            }
            
            self.curPage = 1;
            [self.tableView.mj_header endRefreshing];
            if (self.dataAry.count < 10) {
                
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer setState:MJRefreshStateIdle];
                self.tableView.mj_footer.hidden = NO;
            }
            
            [self.tableView  reloadData];
        }
        
        if (0 == self.dataAry.count) {
            [self showNoDataFlag:YES];
            
        }else{
            [self showNoDataFlag:NO];
        }
        
        [self.tableView.mj_header endRefreshing];
    }];
}


-(void)getMoreOrder
{
    self.curPage++;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(self.curPage);
    
    [YAYINetWorking  getWithURL:noPay_list_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回：%@", dict);
        
        NSString *status = dict[@"status"];
        if ([status isEqualToString:@"success"]) {
            NSArray *data = dict[@"data"];
            for (NSDictionary *obj in data) {
                YAYINoPayOrderModel *model = [YAYINoPayOrderModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataAry  addObject:model];
            }
            
            if (data.count < 10) {
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
            [self.tableView  reloadData];
        }
    }];
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"indentifeer";
    YAYIWaitPayCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIWaitPayCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    cell.model = self.dataAry[indexPath.row];
    cell.button.tag = indexPath.row;
    [cell.button setTitle:@"修改金额" forState:UIControlStateNormal];
    [cell.button setImage:[UIImage imageNamed:@"editMoney"] forState:UIControlStateNormal];
    [cell.button addTarget:self action:@selector(alertMoney:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 244;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIOrderdetailController *order = [YAYIOrderdetailController new];
    YAYINoPayOrderModel *model = self.dataAry[indexPath.row];
    order.title = @"收费详情";
    order.orderid = model.id;
    order.patientname = model.patientname;
    order.clientname = model.userfullname;
    order.avatar = model.userAvatar;
    order.mobile = model.patientMobile;
    order.hospital = model.hospitalname;
    order.totalPrice = model.totalprice;
    order.state = model.state;
    order.reducePrice = [NSString stringWithFormat:@"%d",([model.totalprice intValue] - [model.reduce intValue])];
    order.reduce = model.reduce;
//    order.patientid = model.patientid;
    order.userid = model.userid;
    [self.navigationController pushViewController:order animated:YES];
    
}


/** 修改金额 */
-(void)alertMoney:(UIButton *)sender
{
    YAYIApliyDetailController *controller = [YAYIApliyDetailController new];
    YAYINoPayOrderModel *model = self.dataAry[sender.tag];
    
    controller.hospitalid = model.hospitalid;
    controller.userid = model.userid;
    controller.bookingid = model.bookingid;
    controller.hospital = model.hospitalname;
    controller.avatar = model.userAvatar;
    controller.patientname = model.patientname;
    controller.clientname = model.userfullname;
    controller.totalPrice = [model.totalprice  intValue];
    controller.reducePrice = [model.reduce   intValue];
    controller.ordercontent = model.ordercontent;
    controller.mobile = model.patientMobile;
    controller.orderid = model.id;
    controller.userid = model.userid;
    
    __block typeof(self) weakself = self;
    controller.refreshOperation = ^(){
    
        [weakself.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:controller animated:YES];
    
}

@end
